import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

/*
线程池
 */
class MyThreadPoolExecutor{
    private List<Thread> threadList=new ArrayList<>();
    private BlockingQueue<Runnable> queue=new ArrayBlockingQueue<>(100);
    public MyThreadPoolExecutor(int n){
        for (int i=0;i<n;i++){
            Thread t=new Thread(()->{
                while (true){
                    try {
                        Runnable runnable=queue.take();
                        runnable.run();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            });
            t.start();
            threadList.add(t);
        }
    }
    public void submit(Runnable runnable) throws InterruptedException {
        queue.put(runnable);
    }
}
















/*
阻塞队列

class MyBlockingQueue<T>{
    private Object[] elems=null;

    public MyBlockingQueue(int capcity){
        elems=new Object[capcity];
    }

    private int head;
    private int tail;
    private int count;
    private Object locker=new Object();

    public void put(T t) throws InterruptedException {
       synchronized (locker){
           while (count==elems.length){
               locker.wait();
           }
           elems[tail]=t;
           tail++;
           if (tail==elems.length){
               tail=0;
           }
           count++;
           locker.notify();
       }

    }


    public T take() throws InterruptedException {
        T t=null;
        synchronized (locker){
            while (count==0){
                //dd
                locker.wait();
            }
            t= (T) elems[head];
            head++;
            if (head==elems.length){
                head=0;
            }
            count--;
        }
        locker.notify();
        return t;
    }
}*/













/*
import java.util.PriorityQueue;

*/
/*
定时器
 *//*

class MyTimertask implements Comparable<MyTimertask>{
    private long time;
    private Runnable runnable;

    public MyTimertask(Runnable runnable,long delay){
        this.runnable=runnable;
        this.time=System.currentTimeMillis()+delay;
    }
    public void run(){
        runnable.run();
    }

    public long getTime() {
        return time;
    }

    @Override
    public int compareTo(MyTimertask o) {
        return (int) (this.time-o.time);
    }
}

class MyTImer{
    private Thread t;
    private PriorityQueue<MyTimertask> queue=new PriorityQueue();
    private Object locker=new Object();


    public void schedule(Runnable runnable,long delay){
        MyTimertask task=new MyTimertask(runnable,delay);
        queue.offer(task);
    }

    public MyTImer(){
        t=new Thread(()->{
           try {
               synchronized (locker){
                   while (true){
                       while (queue.isEmpty()){
                           locker.wait();
                       }
                       MyTimertask curTask=queue.peek();
                       if (System.currentTimeMillis()>=curTask.getTime()){
                           queue.poll();
                           curTask.run();
                       }else {

                       }
                   }
               }
           }catch (InterruptedException e){
               e.printStackTrace();
           }
        });
        t.start();
    }
}










*/
/*
懒汉模式
线程不安全
双重校验锁
 *//*



class SingletonLazy{
    private static volatile SingletonLazy instance=null;
    private static Object locker=new Object();
    public static SingletonLazy getInstance() {
        if (instance==null){
            synchronized (locker){
                if(instance==null){

                    instance=new SingletonLazy();
                }
            }
        }
        return instance;
    }

    private SingletonLazy(){}
}






*/
/**
 * 饿汉模式
 * 天然线程安全
 *//*

class Singleton{
    private static Singleton instance=new Singleton();

    public static Singleton getInstance() {
        return instance;
    }
    private Singleton(){}
}







public class Test {
}
*/
